package com.jfinal.weixin.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.core.Controller;
import com.jfinal.weixin.model.*;
import com.jfinal.weixin.sdk.api.*;
import com.jfinal.weixin.sdk.utils.StringUtil;
import com.jfinal.weixin.template.DataItem;
import com.jfinal.weixin.template.TempItem;
import com.jfinal.weixin.template.TempToJson;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 446705260@qq.com on 16/5/16.
 * chengzhichao
 */
public class TestController extends Controller {

    public final static WeixinInfo weixinInfo = WeixinInfo.dao.getWeixinInfoById(1);

    public static DataItem getdata(Weather weather) {
        DataItem dataItem = new DataItem();
        dataItem.setFirst(new TempItem(weather.getStr("title"), "#FF0000"));
        dataItem.setKeyword1(new TempItem(weather.getStr("temp"), "#8CEA00"));
        dataItem.setKeyword2(new TempItem(weather.getStr("cur"), "#8CEA00"));
        dataItem.setKeyword3(new TempItem(weather.getStr("light"), "#8CEA00"));
        dataItem.setKeyword4(new TempItem(weather.getStr("cloud"), "#8CEA00"));
        dataItem.setKeyword5(new TempItem(weather.getStr("suggest"), "#8CEA00"));
        dataItem.setRemark(new TempItem("❤❤❤❤❤❤❤❤❤❤❤❤❤\n"+weather.getStr("desc")+",取消订阅或者恢复订阅请回复《修改订阅状态》\n❤❤❤❤❤❤❤❤❤❤❤❤❤", "#FF0000"));
        return dataItem;
    }

    public static int sendone(String openid) {

        ApiConfigKit.setThreadLocalApiConfig(new ApiConfig(weixinInfo));
        if (!openid.equals(StringUtil.CZC)) {
            return 0;
        }
        String templateid = weixinInfo.getStr("weather_temp_id");
        Weather weather = Weather.dao.findFirst("select * from weather order by datatime desc");
        DataItem dataItem = getdata(weather);
        List<User> list = User.dao.find("select openid,nickname from user where subscribe=1 and dingyue = 0");
        for (int i = 0; i < list.size(); i++) {
            openid = list.get(i).toString();
             String jsonStr = TempToJson.getTempJson(list.get(i).getStr("openid"), templateid,
                "#743A3A", weather.getStr("url"), dataItem);
            ApiResult result = TemplateMsgApi.send(jsonStr);
            System.out.println(list.get(i).get("nickname", "未知用户") + result.getJson() + i);
        }
        return 1;
    }

    public static int send446705260(String openid) {
        if (!openid.equals(StringUtil.CZC)) {
            return 0;
        }
        RedPackApiController redPackApiController = new RedPackApiController();
        ApiConfigKit.setThreadLocalApiConfig(new ApiConfig(WeixinInfo.dao.getWeixinInfoById(1)));
        //List<User> list = User.dao.find("select * from user where user.openid NOT  IN  ( SELECT openid from redpack_log) and subscribe=1");
        List<User> list = User.dao.find("select * from user where subscribe=1 and score >=10");
        StringBuffer stringBuffer = new StringBuffer("");
        for (int i = 0; i < list.size(); i++) {
            String result1 = redPackApiController.send(list.get(i).get("openid").toString(), "红安人专属", "红安人专属");
            stringBuffer.append(result1);
        }
        return 1;
    }

    public static int bill2db(String openid) {
        if (!openid.equals(StringUtil.CZC)) {
            return 0;
        }
        RedPackApiController redPackApiController = new RedPackApiController();
        ApiConfigKit.setThreadLocalApiConfig(new ApiConfig(WeixinInfo.dao.getWeixinInfoById(1)));
        List<RedPackLog> redPackLogList = RedPackLog.dao.find("select * from redpack_log");

        for (int i = 0; i < redPackLogList.size(); i++) {
            RedPackLog redPackLog = redPackLogList.get(i);
            redPackLog.set("openid", redPackApiController.query(redPackLog.get("mch_billno").toString()).get("openid").toString());
            redPackLog.set("result_code", redPackApiController.query(redPackLog.get("mch_billno").toString()).get("result_code").toString());
            redPackLog.update();
        }
        return 1;
    }

    public static int sendto(String openid) {
        if (!openid.equals(StringUtil.CZC)) {
            return 0;
        }
        Weather weather = Weather.dao.findFirst("select * from weather order by datatime desc");
        ApiConfigKit.setThreadLocalApiConfig(new ApiConfig(WeixinInfo.dao.getWeixinInfoById(1)));
        //String openid = "oyHNpv3ChXLAVWw0VpZ0RAGZ7WRc";
        openid = StringUtil.CZC;//程智超
        String templateid = weixinInfo.getStr("weather_temp_id");
        DataItem dataItem = getdata(Weather.dao.findFirst("select * from weather order by datatime desc"));
        String apiResult = UserApi.getFollows().getJson();
        JSONObject jobj = JSONObject.parseObject(apiResult);
        apiResult = jobj.get("data").toString();
        jobj = JSONObject.parseObject(apiResult);
        apiResult = jobj.get("openid").toString();
        JSONArray data = JSONArray.parseArray(apiResult);
        String jsonStr = TempToJson.getTempJson(openid, templateid,
                "#743A3A", weather.getStr("url"), dataItem);
        ApiResult result = TemplateMsgApi.send(jsonStr);
        System.out.println(result.getJson());
        return 1;
    }


    public void kf(){
        ApiConfigKit.setThreadLocalApiConfig(new ApiConfig(WeixinInfo.dao.getWeixinInfoById(1)));
       CustomServiceApi.sendText(StringUtil.CZC,"this is a test");
    }

    public void updatembvote(){
        List<MbUser> mbUserList=  MbUser.dao.find("select * from mbuser");
        MbUser mbUser;
        for(int i=0;i<mbUserList.size();i++){
            mbUser=mbUserList.get(i);
            int id = mbUser.getInt("id");
            System.out.println(id+"已经更新");
            mbUser.set("curscore", MbVote.dao.getScoreOfSomeOne(mbUser.getInt("id")));
            mbUser.update();
        }
    }



}
